# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 14:30:34 2020

@author: 郭国庆
"""


import numpy as np

#逻辑与数据
samples_and = [
                [0,0,0],
                [1,0,0],
                [0,1,0],
                [1,1,1],]

#逻辑或数据
samples_or = [
                [0,0,0],
                [1,0,1],
                [0,1,1],
                [1,1,1],]

#逻辑异或数据
samples_xor = [
                [0,0,0],
                [1,0,1],
                [0,1,1],
                [1,1,0],]

def perceptron(samples):
    w = np.array([1, 2]) # w 权重
    b = 0 #偏置
    a = 1
    
    for i in range(10):#训练10遍， 0-9共10个数
        for j in range(4):#训练4遍，0-3共4个数值
            x = np.array(samples[j][:2])#矩阵的第j行的前两个数值
            y = 1 if np.dot(w, x) + b > 0 else 0 
            #将未激活的值输入sigmoid函数dot：向量的点乘运算
            #当wx+b>0时输出y=1，反之为0
            
            d = np.array(samples[j][2])#真实值
            delta_b = a*(d - y)#偏置b的delta
            delta_w = a*(d - y)*x

            print('epoch {}  sample {}  [{}  {}  {}  {}  {}  {}  {}]'.format(i, j, w[0], w[1], b, y, delta_w[0], delta_w[1], delta_b))
            w = w + delta_w
            b = b + delta_b
        
print('logical and')
perceptron(samples_and)
print('logical or')
perceptron(samples_or)
print('logical xor')
perceptron(samples_xor)




































